using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Markup;
using System.Xml;

using ViewOnCore.Helper.WPF;
using ViewOnCore.Device;
using ViewOnCore.HostContract.Media;
using ViewOnCore.HostContract;
using RadioPlayerAddIn.Properties;

namespace RadioPlayerAddIn
{

    /// <summary>
    /// This dialog allows to edit an online radio.
    /// </summary>
	public partial class EditRadioDialog : System.Windows.Controls.UserControl
	{

		#region Variables

		public bool IsOk = false;
		public Radio Radio;

		#endregion

		#region Constructor

		public EditRadioDialog(Radio radio)
		{
			InitializeComponent();

			Radio = radio;

			btnOk.IsEnabled = false;
			btnCancel.IsEnabled = false;

			//---- The genres
            foreach (RadioGenre genre in Host.MediaService.CurrentMediaDeviceFeature.MediaDatas)
				cboGenres.Items.Add(genre.Title);

			cboGenres.SelectedIndex = 0;

			string[] bitRates = new string[] { "24", "32", "40", "56", "64", "96", "128", "160", "192" };
			foreach (string rate in bitRates)
                cboBitRates.Items.Add(rate + " " + Globalization.EditRadioDialog_KBPS);

			cboBitRates.SelectedIndex = 0;

			//---- If Update mode
			if (Radio != null)
			{
				txtName.Text = Radio.Title;
				cboGenres.SelectedValue = Radio.Genre;
				cboBitRates.SelectedValue = Radio.BitRate;
				txtDescription.Text = Radio.Description;
				txtSreamURL.Text = Radio.StreamUrl;
				txtSiteURL.Text = Radio.SiteUrl;
				txtLanguage.Text = Radio.Language;
				txtCountry.Text = Radio.Country;
				cboPriorities.SelectedIndex = Radio.Priority - 1;
			}

#if !IS_ADMINISTRATIVE_MODE
            InitializeGuestMode();
#endif

			this.childWindowDecorator.VisuallyReady += new EventHandler(childWindowDecorator_VisuallyReady);
		}

		private void InitializeGuestMode()
		{
			txtCountry.Visibility = Visibility.Collapsed;
			txtLanguage.Visibility = Visibility.Collapsed;
			cboPriorities.Visibility = Visibility.Collapsed;
			lblCountry.Visibility = Visibility.Collapsed;
			lblLanguage.Visibility = Visibility.Collapsed;
			lblPriorities.Visibility = Visibility.Collapsed;

			griContent.RowDefinitions.RemoveAt(8);
			griContent.RowDefinitions.RemoveAt(7);
			griContent.RowDefinitions.RemoveAt(6);

			this.Height = this.Height - 75;
		}

		void childWindowDecorator_VisuallyReady(object sender, EventArgs e)
		{
			btnOk.IsEnabled = true;
			btnCancel.IsEnabled = true;
		}

		#endregion

		#region Ok / Cancel

		private void btnOk_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			IsOk = true;

			if (Radio == null)
				Radio = new Radio();

#if IS_ADMINISTRATIVE_MODE
			Radio.Source = RadioSource.ViewOn;
#else
            Radio.Source = RadioSource.User;
#endif

			Radio.Title = txtName.Text.Trim();
			Radio.Genre = cboGenres.SelectedValue.ToString();
			Radio.BitRate = cboBitRates.SelectedValue.ToString();
			Radio.Description = txtDescription.Text.Trim();
			Radio.StreamUrl = txtSreamURL.Text.Trim();
			Radio.SiteUrl = txtSiteURL.Text.Trim();
#if IS_ADMINISTRATIVE_MODE
			Radio.Language = txtLanguage.Text.Trim();
			Radio.Country = txtCountry.Text.Trim();
			Radio.Priority = cboPriorities.SelectedIndex + 1;
#else
			Radio.Language = "en";
			Radio.Country = "us";
			Radio.Priority = 15;
#endif

			//---- Close the dialog
			childWindowDecorator.Close();
		}

		private void btnCancel_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			//---- Close the dialog
			childWindowDecorator.Close();
		}

		#endregion

        #region ShowDialog

        /// <summary>
        /// Show the dialog.
        /// </summary>
        /// <param name="container">The desktop</param>
        /// <param name="handler">This handler handle when the dialog is closed</param>
        public void ShowDialog(Desktop container, ChildWindowDialogClosedHandler handler)
        {
            childWindowDecorator.ShowDialog(container, handler);
        }

        #endregion

	}

}